package cn.gjculture.shopping.api.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.LinkedHashMap;
import java.util.Map;

@Slf4j
public class HttpClient {

    public static String client(String url, HttpMethod method, MultiValueMap<String, String> param){
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(param, headers);
        //  执行HTTP请求
        ResponseEntity<String> response = client.exchange(url, method, requestEntity, String.class);
        return response.getBody();
    }

    public static MultiValueMap<String, String>  getParam(){

        //数据加密
       // JSONObject returnJson = new JSONObject();
        Map<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("sellerNo","0001514");
        resultMap.put("payChannels","{\"fastpay\":true,\"weChatPay\":false,\"weChatPayMobile\":true,\"aliPay\":false,\"aliPayMobile\":true,\"balancePay\":false}");
        //resultMap.put("orderBody","Iphone手机一部");
        resultMap.put("payAmount","120.11");
        resultMap.put("apiPayType","1");
        resultMap.put("tradeType", "0");
        resultMap.put("merMerOrderNo",UUIDUtil.getUUID());
//        resultMap.put("buyerNo","");
        resultMap.put("undiscountableAmount","");
        resultMap.put("orderSubject","商品支付");
//        JSONObject obj=new JSONObject();
       // obj.put("routingMerNo","");
//        obj.put("routingFee","");
        //resultMap.put("tempRoutingList","["+obj.toJSONString()+"]");
        //resultMap.put("tempCommissionList", "");
//        obj.clear();
//        obj.put("sqGoodsId","");
//        obj.put("goodsName","");
//        obj.put("quantity","");
//        obj.put("price","");
//        obj.put("goodsCategory","");
//        obj.put("body","");
//        obj.put("showUrl","");
       // resultMap.put("goodsDetail",obj.toJSONString());
        resultMap.put("returnUrl","http://zevi.free.idcfengye.com/pay/back");
        resultMap.put("ptUndertakeRate","");

        Map<String, Object> requestMap = new LinkedHashMap<>();
        requestMap.put("merNo","0001514");
        requestMap.put("version","1.1");
        requestMap.put("notifyUrl","http://zevi.free.idcfengye.com/pay/back");
        String timestamp = DateUtils.getDateTime();
        requestMap.put("timestamp",timestamp);
        requestMap.put("apiContent",JSON.toJSONString(resultMap));
        requestMap.put("signType","CFCA");
        String beforeSignedData = CFCARAUtil.joinMapValue(requestMap, '&');
        log.info("签名前数据集:{}",beforeSignedData);
        String sign = "";
        String pfxPath = "key/dev/0001514-test-sign.pfx";
        String passWord = "123456";
        log.info(beforeSignedData);
        try {
            sign = CFCARAUtil.signMessageByP1(beforeSignedData,pfxPath, passWord);
            log.info("客户端-加密后数据--" + sign);
        } catch (Exception e2) {
            e2.printStackTrace();
        }
        MultiValueMap<String, String> params= new LinkedMultiValueMap<>();
        params.add("merNo", "0001514");
        params.add("signType", "CFCA");
        params.add("version","1.1");
        params.add("notifyUrl", "http://zevi.free.idcfengye.com/pay/back");
        log.info("sign:{}",sign);
        params.add("sign", sign);
        log.info("timestamp:{}",timestamp);
        params.add("timestamp",timestamp);
        String apiContent= JSON.toJSONString(resultMap);
        log.info("apiContent:{}",apiContent);
        params.add("apiContent", apiContent);
        return params;
        //最后将returnJson中的几个公共请求参数提交至双乾即可
    }
    public static void main(String[] args) {
        //api url地址
        String url="http://shoudan.95epay.com:9000/api/api/hPay/toPayHtml";
        //post请求
        HttpMethod method =HttpMethod.POST;
        // 封装参数，千万不要替换为Map与HashMap，否则参数无法传递

      //  params.add("access_token", "xxxxx");
        //发送http请求并返回结果
       String response=HttpClient.client(url,method,getParam());
    }

    public static String get(String url){
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> params= new LinkedMultiValueMap<>();
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
        ResponseEntity<String> response = client.exchange(url, HttpMethod.GET, requestEntity, String.class);
        return response.getBody();
    }

    public static String postJson(String url, JSONObject requestBody){
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        HttpEntity<JSONObject> requestEntity = new HttpEntity<>(requestBody,headers);
        ResponseEntity<String> response = client.exchange(url, HttpMethod.POST, requestEntity, String.class);
        return response.getBody();
    }
}
